# Maintainer: Filipe Laíns (FFY00) <filipe.lains@gmail.com>

pkgbase=libad9361
pkgname=(libad9361 libad9361-docs)
_name=$pkgbase-iio
pkgver=0.3
pkgrel=1
pkgdesc='IIO AD9361 library for filter design and handling, multi-chip sync, etc.'
arch=(loong64 x86_64)
url="https://github.com/analogdevicesinc/libad9361-iio"
license=(LGPL2.1)
makedepends=(cmake doxygen graphviz libiio)
source=($url/archive/v$pkgver/$_name-v$pkgver.tar.gz)
sha512sums=('904f81fead71a9c93ccdcdb8822247fc8c496920abb9b13ea4b2b0465d50f5c0115ffc8d226496e76f7bb23b466031556129b18a1f35c65c30dcb3b0c1aeeff9')
b2sums=('bdd3733ece66f1b34075821316ce054537059297964cc4bd92bc4c768d1253a2b6d1822bf6cce20082395b0f7d72ccc63a5002608f8042494a6a543a3e0369a4')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D ENABLE_PACKAGING=OFF
    -S $_name-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

package_libad9361() {
  depends=(glibc libiio)
  optdepends=(
    'libad9361-docs: for documentation'
  )

  DESTDIR="$pkgdir" cmake --install build

  (
    cd "$pkgdir"
    _pick $pkgbase-docs usr/share/doc
  )
}

package_libad9361-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir"
}
